<?php

use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
use PhpAmqpLib\Wire\AMQPTable;

require __DIR__ . '/../../vendor/autoload.php';

$config = require(__DIR__ . '/../config.php');
$connection = new AMQPStreamConnection(
    $config['host'],
    $config['port'],
    $config['user'],
    $config['password'],
    $config['vhost']
);

$channel = $connection->channel();

const DELAY_EXCHANGE = 'delay_exchange';
const DELAY_QUEUE = 'delay_queue';

$channel->exchange_declare(DELAY_EXCHANGE, 'x-delayed-message', false, false, false, false, false, new AMQPTable([
    'x-delayed-type' => 'direct',
]));
$channel->queue_declare(DELAY_QUEUE, false, false, false, false, false);
$channel->queue_bind(DELAY_QUEUE, DELAY_EXCHANGE, 'rk_delay');

$channel->basic_consume(DELAY_QUEUE, '', false, true, false, false, function($msg) {
    echo $msg->body . PHP_EOL;
});

while ($channel->is_open()) {
    $channel->wait();
}

$channel->close();
$connection->close();